package wms.camera.project;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;

public class AndroidCameraProjectActivity extends Activity implements OnClickListener, SurfaceHolder.Callback{
	
	private Button startCamera, stopCamera, takePicture;
	private Camera camera;
	private boolean previewing;
	private SurfaceView surfaceView;
	private SurfaceHolder surfaceHolder;
	private RelativeLayout layout;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        previewing = false;
        
        //Initialize GUI objects
        layout = (RelativeLayout) findViewById(R.id.main);
        startCamera = (Button) findViewById(R.id.startCamera);
        startCamera.setOnClickListener(this);
        stopCamera = (Button) findViewById(R.id.stopCamera);
        stopCamera.setOnClickListener(this);
        takePicture = (Button) findViewById(R.id.takePicture);
        takePicture.setOnClickListener(this);
        
        //Set up preview window
        getWindow().setFormat(PixelFormat.UNKNOWN);
        surfaceView = (SurfaceView)findViewById(R.id.camPreview);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
    }
    
    public void onClick(View v) {
    	
    	//Check which button is pressed
    	if (v.getId() == R.id.startCamera) {
    		camera = Camera.open();
    		
    		//Display window with camera preview
    		if (!previewing) {
    			try {
    				camera.setPreviewDisplay(surfaceHolder);
    				camera.startPreview();
    				previewing = true;
    			} catch(IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    	else if (v.getId() == R.id.stopCamera) {
    		if (camera != null && previewing) {
    			//Stop camera preview and close camera
    			stopCamera();
    		}
    	}
    	else if (v.getId() == R.id.takePicture) {
			if (camera != null && previewing)
    			camera.takePicture(shutterCallback, rawCallback, jpegCallback);
    	}
    }
    
    private void stopCamera() {
    	camera.stopPreview();
		camera.release();
		camera = null;
		previewing = false;
    }
    
    /* Picture callback objects */
    private ShutterCallback shutterCallback = new ShutterCallback(){
        @Override
        public void onShutter() { }
     };
     
    PictureCallback rawCallback = new PictureCallback(){
        @Override
        public void onPictureTaken(byte[] data, Camera camera) { }
    };
    
    PictureCallback jpegCallback = new PictureCallback(){
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
        	
            FileOutputStream fos = null;
            File picFile = null;
            File fileDir = null;
            
            try{
            	fileDir = new File("/sdcard/images");
            	fileDir.mkdirs();
            	picFile = new File(fileDir, "pic.jpg");
            	if (picFile.exists())
            		picFile.delete();
            	
            	fos = new FileOutputStream(picFile);
            	fos.write(data);
            	fos.close();
            }catch(FileNotFoundException e){
                Log.d("Camera", e.getMessage());
            } catch (IOException e) {
                Log.d("Camera", e.getMessage());
            }
            
            //Stop camera preview and change view
            stopCamera();
            Intent displayIntent = new Intent(layout.getContext(), PictureDisplayActivity.class);
			startActivityForResult(displayIntent, 0);
        }
    };

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }

	@Override
	public void surfaceCreated(SurfaceHolder holder) { }

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) { }
}